/*
 * FileName：WebsiteIndexCtrl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.wx.ctrl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.jpay.ext.kit.ZxingKit;
import com.smart.common.util.*;
import com.smart.wx.wxapi.process.*;
import com.smart.wx.wxcms.entity.*;
import com.smart.wx.wxcms.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.smart.common.BaseCtrl;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 微官网微信端
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-11 20:58:43
 */
@Controller
@RequestMapping("/front/websiteindex")
public class WebsiteIndexCtrl extends BaseCtrl {

    @Resource
    private WebsiteBannerService websiteBannerService;

    @Resource
    private WebsiteBrandIntroductionService websiteBrandIntroductionService;

    @Resource
    private WebsiteServiceIntroductionService websiteServiceIntroductionService;

    @Resource
    private WebsiteNewsCenterService websiteNewsCenterService;

    @Resource
    private WebsiteContactUsService websiteContactUsService;

    @Resource
    private WebsiteMessageBoardService websiteMessageBoardService;


    /**
     * 生成链接
     */
    @ResponseBody
    @RequestMapping("/generate")
    public AjaxResult generate() throws Exception {

        MpAccount mpAccount = WxMemoryCacheClient.getMpAccount();
        String account = mpAccount.getAccount();

        String domainName = PropertiesUtil.getString("domain.name").toString();
        String website_index_url = domainName + "/front/websiteindex/" + account;


        // 生成二维码
        String fileName = account + ".png";
        String imgBasePath = UploadUtil.getImgBasePath(account + ".png");

        ZxingKit.encode(website_index_url, BarcodeFormat.QR_CODE, 3, ErrorCorrectionLevel.H, "png", 200, 200, imgBasePath);

        String picUrl = PropertiesUtil.getString("res.upload.url") + fileName;

        Map<String, String> resMap = new HashMap<String, String>();
        resMap.put("website_index_url", website_index_url);
        resMap.put("picUrl", picUrl);

        return AjaxResult.success(resMap);
    }


    /**
     * 微官网首页[轮播图]
     */
    @ResponseBody
    @RequestMapping("/getWebsiteBannerList")
    public AjaxResult getWebsiteBannerList(String account) throws Exception {

        WebsiteBanner websiteBanner = new WebsiteBanner();
        websiteBanner.setAccount(account);
        List<WebsiteBanner> list = this.websiteBannerService.queryList(websiteBanner);

        return AjaxResult.success(list);
    }


    /**
     * 微官网首页[品牌介绍]
     */
    @ResponseBody
    @RequestMapping("/getWebsiteBrandIntroductionList")
    public AjaxResult getWebsiteBrandIntroductionList(String account) throws Exception {

        WebsiteBrandIntroduction websiteBrandIntroduction = new WebsiteBrandIntroduction();
        websiteBrandIntroduction.setAccount(account);
        List<WebsiteBrandIntroduction> list = this.websiteBrandIntroductionService.queryList(websiteBrandIntroduction);

        return AjaxResult.success(list);
    }


    /**
     * 微官网首页[服务介绍]
     */
    @ResponseBody
    @RequestMapping("/getWebsiteServiceIntroductionList")
    public AjaxResult getWebsiteServiceIntroductionList(String account) throws Exception {

        WebsiteServiceIntroduction websiteServiceIntroduction = new WebsiteServiceIntroduction();
        websiteServiceIntroduction.setAccount(account);
        List<WebsiteServiceIntroduction> list = this.websiteServiceIntroductionService.queryList(websiteServiceIntroduction);

        return AjaxResult.success(list);
    }

    /**
     * 微官网首页[服务介绍]详情
     */
    @ResponseBody
    @RequestMapping("/getWebsiteServiceIntroductionInfo")
    public AjaxResult getWebsiteServiceIntroductionInfo(String id) throws Exception {

        WebsiteServiceIntroduction websiteServiceIntroduction = new WebsiteServiceIntroduction();
        websiteServiceIntroduction.setId(id);
        websiteServiceIntroduction = this.websiteServiceIntroductionService.queryObject(websiteServiceIntroduction);

        return AjaxResult.success(websiteServiceIntroduction);
    }


    /**
     * 微官网首页[新闻中心]
     */
    @ResponseBody
    @RequestMapping("/getWebsiteNewsCenterList")
    public AjaxResult getWebsiteNewsCenterList(WebsiteNewsCenter websiteNewsCenter) throws Exception {

//        WebsiteNewsCenter websiteNewsCenter = new WebsiteNewsCenter();
//        websiteNewsCenter.setAccount(account);
        List<WebsiteNewsCenter> list = this.websiteNewsCenterService.queryListByPage(websiteNewsCenter);

        return getResult(websiteNewsCenter, list);
    }

    /**
     * 微官网首页[新闻中心]详情
     */
    @ResponseBody
    @RequestMapping("/getWebsiteNewsCenterInfo")
    public AjaxResult getWebsiteNewsCenterInfo(String id) throws Exception {

        WebsiteNewsCenter websiteNewsCenter = new WebsiteNewsCenter();
        websiteNewsCenter.setId(id);
        websiteNewsCenter = this.websiteNewsCenterService.queryObject(websiteNewsCenter);

        return AjaxResult.success(websiteNewsCenter);
    }


    /**
     * 微官网首页[联系我们]
     */
    @ResponseBody
    @RequestMapping("/getWebsiteContactUsList")
    public AjaxResult getWebsiteContactUsList(String account) throws Exception {

        WebsiteContactUs websiteContactUs = new WebsiteContactUs();
        websiteContactUs.setAccount(account);
        List<WebsiteContactUs> list = this.websiteContactUsService.queryList(websiteContactUs);

        return AjaxResult.success(list);
    }

    /**
     * 新增用户留言
     */
    @ResponseBody
    @RequestMapping("/addMessage")
    public AjaxResult addMessage(WebsiteMessageBoard websiteMessageBoard) throws Exception {

        websiteMessageBoard.setId(Util.getUUID());
        websiteMessageBoard.setCreateTime(new Date());

        int i = websiteMessageBoardService.insert(websiteMessageBoard);
        if (i > 0) {
            return AjaxResult.saveSuccess();
        } else {
            return AjaxResult.failure();
        }
    }

    /**
     * 微官网首页
     */
    @RequestMapping("/{account}")
    public String index(@PathVariable String account) {
        return "redirect:/views/front/website/index.html?account=" + account;
    }

}
